﻿Shader "Custom/TestShader25"
{
	Properties
	{
        _Color ("Color", color) = (1,1,1,1)
        _TileFactor ("TileFactor", float) = 1
        _Outline("Outline", float) = 0.1
        _OutlineColor("OutlineColor", color) = (1,1,1,1)
		_Hatch0 ("Hatch0", 2D) = "white" {}
        _Hatch1("Hatch0", 2D) = "white" {}
        _Hatch2("Hatch0", 2D) = "white" {}
        _Hatch3("Hatch0", 2D) = "white" {}
        _Hatch4("Hatch0", 2D) = "white" {}
        _Hatch5("Hatch0", 2D) = "white" {}
    }
    SubShader
    {
        UsePass "Custom/TestShader24/OUTLINE"

        Pass
        {
            Tags {"LightMode" = "ForwardBase"}

            CGPROGRAM
            #pragma multi_compile_fwdbase
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"
            #include "AutoLight.cginc"
            sampler2D _Hatch0;
            sampler2D _Hatch1;
            sampler2D _Hatch2;
            sampler2D _Hatch3;
            sampler2D _Hatch4;
            sampler2D _Hatch5;
            float _TileFactor;
            fixed4  _Color;


            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 uv : TEXCOORD0;

            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                fixed3 hatchWeight0 : TEXCOORD1;
                fixed3 hatchWeight1 : TEXCOORD2;
                float3 worldPos : TEXCOORD3;
                SHADOW_COORDS(4)
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv.xy * _TileFactor;
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

                fixed3 worldLightDir = UnityWorldSpaceLightDir(o.worldPos);
                fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                fixed diff = max(0, dot(worldLightDir, worldNormal));
                o.hatchWeight0 = float3(0, 0, 0);
                o.hatchWeight1 = float3(0, 0, 0);

                float hatchFactor = diff * 7;
                if (hatchFactor > 6)
                {

                }
                else if (hatchFactor > 5)
                {
                    o.hatchWeight0.x = hatchFactor - 5;
                }
                else if (hatchFactor > 4)
                {
                    o.hatchWeight0.y = hatchFactor - 4;
                }
                else if (hatchFactor > 3)
                {
                    o.hatchWeight0.z = hatchFactor - 3;
                }
                else if (hatchFactor > 2)
                {
                    o.hatchWeight1.x = hatchFactor - 2;
                }
                else if (hatchFactor > 1)
                {
                    o.hatchWeight1.y = hatchFactor - 1;
                }
                else
                {
                    o.hatchWeight1.z = hatchFactor;
                }

                TRANSFER_SHADOW(o);

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 hatchTex0 = tex2D(_Hatch0, i.uv) * i.hatchWeight0.x;
                fixed4 hatchTex1 = tex2D(_Hatch1, i.uv) * i.hatchWeight0.y;
                fixed4 hatchTex2 = tex2D(_Hatch2, i.uv) * i.hatchWeight0.z;
                fixed4 hatchTex3 = tex2D(_Hatch3, i.uv) * i.hatchWeight1.x;
                fixed4 hatchTex4 = tex2D(_Hatch4, i.uv) * i.hatchWeight1.y;
                fixed4 hatchTex5 = tex2D(_Hatch5, i.uv) * i.hatchWeight1.z;
                fixed4 whiteColor = fixed4(1, 1, 1, 1) * (1 - i.hatchWeight0.x - i.hatchWeight0.y - i.hatchWeight0.z
                    - i.hatchWeight1.x - i.hatchWeight1.y - i.hatchWeight1.z);

                fixed4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5 + whiteColor;

                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
                return fixed4(hatchColor.rgb * _Color.rgb * atten, 1);
            }



            ENDCG
        }
	}
}
